package com.nextive.weightlogger.dao;

import com.nextive.weightlogger.WeightUnit;
import com.nextive.weightlogger.dto.Settings;
import com.nextive.weightlogger.dto.Settings.RecipientType;



import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;

/**
 * Data access object to read and save from {@link SharedPreferences} the user
 * settings for this application.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public class SettingsDao {
	/**
	 * Name of the shared preferences file to use
	 */
	private static final String SETTINGS_FILE = "settings";
	
	// Shared preferences names
	private static final String SETTING_NAME = "name";
	private static final String SETTING_EMAIL = "email";
	private static final String SETTING_DEFAULT_RECIPIENT = 
		"defaultRecipient";
	private static final String SETTING_DEFAULT_RECIPIENT_EMAIL = 
		"defaultRecipientEmail";
	private static final String SETTING_UNIT = "unit";
	private static final String SETTING_VALID = "valid";
	
	private SharedPreferences sharedPreferences;

	/**
	 * Constructor.
	 * 
	 * @param context
	 *            The ContextWrapper or ContextWrapper subclass to get the
	 *            shared preferences from, usually the current Activity.
	 */
	public SettingsDao(ContextWrapper context)
	{
		sharedPreferences = context.getSharedPreferences(SETTINGS_FILE,
				Context.MODE_PRIVATE);
	}

	/**
	 * Retrieves whether the settings were saved as valid settings.
	 * 
	 * @return True if settings were saved as valid, false if settings were
	 *         saved as not valid or if settings were never saved.
	 */
	public boolean getSettingsValid()
	{
		return sharedPreferences.getBoolean(SETTING_VALID, false);
	}
	
	/**
	 * Retrieves the user settings.
	 * 
	 * @return The user settings
	 */
	public Settings readSettings()
	{
		Settings settings = new Settings();
		settings.setName(sharedPreferences.getString(SETTING_NAME, ""));
		settings.setEmail(sharedPreferences.getString(SETTING_EMAIL, ""));
		String recipientTypeString = sharedPreferences.getString(
				SETTING_DEFAULT_RECIPIENT, null);
		if (recipientTypeString != null) {
			settings.setDefaultRecipientType(RecipientType
					.valueOf(recipientTypeString));
		}
		settings.setDefaultRecipientEmail(sharedPreferences.getString(
				SETTING_DEFAULT_RECIPIENT_EMAIL, null));
		settings.setUnit(WeightUnit.valueOf(sharedPreferences.getString(SETTING_UNIT,
				WeightUnit.POUND.name())));
		return settings;
	}
	
	/**
	 * Saves the user settings.
	 * 
	 * @param settings User settings to save
	 * @param valid Whether the settings were successfully validated
	 */
	public void saveSettings(Settings settings, boolean valid){
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(SETTING_NAME, settings.getName());
		editor.putString(SETTING_EMAIL, settings.getEmail());
		editor.putString(SETTING_DEFAULT_RECIPIENT, settings
				.getDefaultRecipientType().name());
		editor.putString(SETTING_DEFAULT_RECIPIENT_EMAIL, settings
				.getDefaultRecipientEmail());
		editor.putString(SETTING_UNIT, settings.getUnit().name());
		editor.putBoolean(SETTING_VALID, valid);
		editor.commit();
	}
}
